home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / mpeg_stat-2.2 / motionvector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  7.3 KB  |  234 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  * 
  3.  *
  4.  *  Copyright (c) 1995 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * Technical University of Berlin, Germany, Dept. of Computer Science
  8.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  9.  *
  10.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  11.  *
  12.  * This program uses parts of the source code of the Berkeley MPEG player
  13.  *
  14.  * ---------------------------
  15.  *
  16.  * Copyright (c) 1993 Technical University of Berlin, Germany
  17.  *
  18.  * for the parts of the Berkeley player used:
  19.  *
  20.  * Copyright (c) 1992 The Regents of the University of California.
  21.  * All rights reserved.
  22.  *
  23.  * ---------------------------
  24.  *
  25.  * Permission to use, copy, modify, and distribute this software and its
  26.  * documentation for any purpose, without fee, and without written agreement is
  27.  * hereby granted, provided that the above copyright notices and the following
  28.  * two paragraphs appear in all copies of this software.
  29.  * 
  30.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  31.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  32.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  33.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  34.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  35.  * POSSIBILITY OF SUCH DAMAGE.
  36.  * 
  37.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  38.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  39.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  41.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  42.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  43.  * OR MODIFICATIONS.
  44.  */
  45. #include "video.h"
  46. #include "proto.h"
  47. #include "util.h"
  48. #include "dither.h"
  49.  
  50. int max_horizontal_f = 0, max_vertical_f = 0, max_horizontal_b = 0, max_vertical_b = 0;
  51.  
  52. /*
  53.  *--------------------------------------------------------------
  54.  *
  55.  * ComputeVector --
  56.  *
  57.  *    Computes motion vector given parameters previously parsed
  58.  *      and reconstructed.
  59.  *
  60.  * Results:
  61.  *      Reconstructed motion vector info is put into recon_* parameters
  62.  *      passed to this function. Also updated previous motion vector
  63.  *      information.
  64.  *
  65.  * Side effects:
  66.  *      None.
  67.  *
  68.  *--------------------------------------------------------------
  69.  */
  70.  
  71. #define ComputeVector(recon_right_ptr, recon_down_ptr, recon_right_prev, recon_down_prev, f, full_pel_vector, motion_h_code, motion_v_code, motion_h_r, motion_v_r)                \
  72.                                     \
  73. {                                    \
  74.   int comp_h_r, comp_v_r;                        \
  75.   int right_little, right_big, down_little, down_big;            \
  76.   int max, min, new_vector;                        \
  77.                                     \
  78.   /* The following procedure for the reconstruction of motion vectors     \
  79.      is a direct and simple implementation of the instructions given    \
  80.      in the mpeg December 1991 standard draft.                 \
  81.   */                                    \
  82.                                     \
  83.   if (f == 1 || motion_h_code == 0)                    \
  84.     comp_h_r = 0;                            \
  85.   else                                     \
  86.     comp_h_r = f - 1 - motion_h_r;                    \
  87.                                     \
  88.   if (f == 1 || motion_v_code == 0)                    \
  89.     comp_v_r = 0;                            \
  90.   else                                     \
  91.     comp_v_r = f - 1 - motion_v_r;                    \
  92.                                     \
  93.   right_little = motion_h_code * f;                    \
  94.   if (right_little == 0)                        \
  95.     right_big = 0;                            \
  96.   else {                                \
  97.     if (right_little > 0) {                        \
  98.       right_little = right_little - comp_h_r;                \
  99.       right_big = right_little - 32 * f;                \
  100.     }                                    \
  101.     else {                                \
  102.       right_little = right_little + comp_h_r;                \
  103.       right_big = right_little + 32 * f;                \
  104.     }                                    \
  105.   }                                    \
  106.                                     \
  107.   down_little = motion_v_code * f;                    \
  108.   if (down_little == 0)                            \
  109.     down_big = 0;                            \
  110.   else {                                \
  111.     if (down_little > 0) {                        \
  112.       down_little = down_little - comp_v_r;                \
  113.       down_big = down_little - 32 * f;                    \
  114.     }                                    \
  115.     else {                                \
  116.       down_little = down_little + comp_v_r;                \
  117.       down_big = down_little + 32 * f;                    \
  118.     }                                    \
  119.   }                                    \
  120.                                       \
  121.   max = 16 * f - 1;                            \
  122.   min = -16 * f;                            \
  123.                                     \
  124.   new_vector = recon_right_prev + right_little;                \
  125.                                     \
  126.   if (new_vector <= max && new_vector >= min)                \
  127.     *recon_right_ptr = recon_right_prev + right_little;            \
  128.                       /* just new_vector */                \
  129.   else                                    \
  130.     *recon_right_ptr = recon_right_prev + right_big;            \
  131.   recon_right_prev = *recon_right_ptr;                    \
  132.   if (full_pel_vector)                            \
  133.     *recon_right_ptr = *recon_right_ptr << 1;                \
  134.                                     \
  135.   new_vector = recon_down_prev + down_little;                \
  136.   if (new_vector <= max && new_vector >= min)                \
  137.     *recon_down_ptr = recon_down_prev + down_little;            \
  138.                       /* just new_vector */                \
  139.   else                                    \
  140.     *recon_down_ptr = recon_down_prev + down_big;            \
  141.   recon_down_prev = *recon_down_ptr;                    \
  142.   if (full_pel_vector)                            \
  143.     *recon_down_ptr = *recon_down_ptr << 1;                \
  144. }
  145.  
  146. /*
  147.  *--------------------------------------------------------------
  148.  *
  149.  * ComputeForwVector --
  150.  *
  151.  *    Computes forward motion vector by calling ComputeVector
  152.  *      with appropriate parameters.
  153.  *
  154.  * Results:
  155.  *    Reconstructed motion vector placed in recon_right_for_ptr and
  156.  *      recon_down_for_ptr.
  157.  *
  158.  * Side effects:
  159.  *      None.
  160.  *
  161.  *--------------------------------------------------------------
  162.  */
  163.  
  164. void 
  165. ComputeForwVector(recon_right_for_ptr, recon_down_for_ptr)
  166.      int *recon_right_for_ptr;
  167.      int *recon_down_for_ptr;
  168. {
  169.  
  170.   Pict *picture;
  171.   Macroblock *mblock;
  172.  
  173.   picture = &(curVidStream->picture);
  174.   mblock = &(curVidStream->mblock);
  175.  
  176.   ComputeVector(recon_right_for_ptr, recon_down_for_ptr,
  177.         mblock->recon_right_for_prev, 
  178.         mblock->recon_down_for_prev,
  179.         picture->forw_f, picture->full_pel_forw_vector,
  180.         mblock->motion_h_forw_code, mblock->motion_v_forw_code,
  181.         mblock->motion_h_forw_r, mblock->motion_v_forw_r); 
  182.   if (max_horizontal_f < abs(*recon_right_for_ptr))
  183.      max_horizontal_f = abs (*recon_right_for_ptr);
  184.   if (max_vertical_f < abs(*recon_down_for_ptr))
  185.      max_vertical_f = abs (*recon_down_for_ptr);
  186.   mvstat (*recon_right_for_ptr, 0);
  187.   mvstat (*recon_down_for_ptr, 1);
  188. }
  189.  
  190.  
  191. /*
  192.  *--------------------------------------------------------------
  193.  *
  194.  * ComputeBackVector --
  195.  *
  196.  *    Computes backward motion vector by calling ComputeVector
  197.  *      with appropriate parameters.
  198.  *
  199.  * Results:
  200.  *    Reconstructed motion vector placed in recon_right_back_ptr and
  201.  *      recon_down_back_ptr.
  202.  *
  203.  * Side effects:
  204.  *      None.
  205.  *
  206.  *--------------------------------------------------------------
  207.  */
  208.  
  209. void 
  210. ComputeBackVector(recon_right_back_ptr, recon_down_back_ptr)
  211.      int *recon_right_back_ptr;
  212.      int *recon_down_back_ptr;
  213. {
  214.   Pict *picture;
  215.   Macroblock *mblock;
  216.  
  217.   picture = &(curVidStream->picture);
  218.   mblock = &(curVidStream->mblock);
  219.  
  220.   ComputeVector(recon_right_back_ptr, recon_down_back_ptr,
  221.         mblock->recon_right_back_prev, 
  222.         mblock->recon_down_back_prev,
  223.         picture->back_f, picture->full_pel_back_vector,
  224.         mblock->motion_h_back_code, mblock->motion_v_back_code,
  225.         mblock->motion_h_back_r, mblock->motion_v_back_r); 
  226.  
  227.   if (max_horizontal_b < abs(*recon_right_back_ptr))
  228.      max_horizontal_b = abs (*recon_right_back_ptr);
  229.   if (max_vertical_b < abs(*recon_down_back_ptr))
  230.      max_vertical_b = abs (*recon_down_back_ptr);
  231.   mvstat (*recon_right_back_ptr, 2);
  232.   mvstat (*recon_down_back_ptr, 3);
  233. }
  234.